﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Content;

namespace Labirinto_Magico
{
    class GameRender
    {
        RenderingData mRenderWrapper    = null;

        public GameRender(RenderingData iRender)
        {
            mRenderWrapper = iRender;
        }

        void DrawCurvedTerrain(Vector2 iPosition, float iDimension, SI.ORIENTATION iOrientation, float scale, Color color)
        {
            float rotation;
            if (SI.ORIENTATION.S == iOrientation)
                rotation = ((float)Math.PI * 1.5f);
            else if (SI.ORIENTATION.E == iOrientation)
                rotation = ((float)Math.PI);
            else if (SI.ORIENTATION.N == iOrientation)
                rotation = ((float)Math.PI * 0.5f);
            else
                rotation = 0;

            mRenderWrapper.DrawTexture(mRenderWrapper.mTextureData.curvedTile, new Vector2(iDimension, iDimension), iPosition, rotation, color);
        }

        void DrawStreightTerrain(Vector2 iPosition, float iDimension, SI.ORIENTATION iOrientation, float scale, Color color)
        {
            float rotation;

            if (SI.ORIENTATION.E == iOrientation)
                rotation = ((float)Math.PI * 1.5f);
            else if (SI.ORIENTATION.S == iOrientation)
                rotation = ((float)Math.PI);
            else if (SI.ORIENTATION.W == iOrientation)
                rotation = ((float)Math.PI * 0.5f);
            else
                rotation = (0.0f);

            mRenderWrapper.DrawTexture(mRenderWrapper.mTextureData.streightTile, new Vector2(iDimension, iDimension), iPosition, rotation, color);
        }

        void DrawTieTerrain(Vector2 iPosition, float iDimension, SI.ORIENTATION iOrientation, float scale, Color color)
        {
            float rotation;

            if (SI.ORIENTATION.S == iOrientation)
                rotation = ((float)Math.PI * 1.5f);
            else if (SI.ORIENTATION.E == iOrientation)
                rotation = ((float)Math.PI);
            else if (SI.ORIENTATION.N == iOrientation)
                rotation = ((float)Math.PI * 0.5f);
            else
                rotation = (0.0f);

            mRenderWrapper.DrawTexture(mRenderWrapper.mTextureData.tieTile, new Vector2(iDimension, iDimension), iPosition, rotation, color);
        }

        void DrawTile(Vector2 iPosition, float iDimension, SI.TILE_TYPE iType, SI.ORIENTATION iOrientation, Color iColor)
        {
            switch (iType)
            {
                case SI.TILE_TYPE.Corner:
                    DrawCurvedTerrain(iPosition, iDimension, iOrientation, 1.0f, iColor);
                    break;
                case SI.TILE_TYPE.Curved:
                    DrawCurvedTerrain(iPosition, iDimension, iOrientation, 1.0f, iColor);
                    break;
                case SI.TILE_TYPE.Streight:
                    DrawStreightTerrain(iPosition, iDimension, iOrientation, 1.0f, iColor);
                    break;
                case SI.TILE_TYPE.Tie:
                    DrawTieTerrain(iPosition, iDimension, iOrientation, 1.0f, iColor);
                    break;
            }
        }

        public void DrawChessboard(SI.LOGIC_GAME_STATE stateMachine, Tile.Tile iSelectedTile, Tile.Tile iRemainedTile)
        {
            LabyrinthGameState.State iState = LabyrinthGameState.gameState;
            Player.Player iSelectedPlayer = iState.mSelectedPlayer;
            float startHeight = 0;
            float tileLenght = iRemainedTile.mDimension;
            bool showPath = (stateMachine == SI.LOGIC_GAME_STATE.P || stateMachine == SI.LOGIC_GAME_STATE.M);

            for (int i = 0; i < 7; ++i)
                for (int j = 0; j < 7; ++j)
                {
                    Tile.Tile tile = iState.mChessboard.board[i * 7 + j];
                    Vector2 position = new Vector2(j * tileLenght + tileLenght * 1.5f, startHeight + i * tileLenght + tileLenght);
                    position += new Vector2(tile.mDimension * 0.5f, tile.mDimension * 0.5f);

                    if (!iState.mFirstPart && tile.Equals(iSelectedTile))
                        DrawTile(position, tile.mDimension, tile.mType, tile.mOrientation, Color.Gray);
                    else if (iState.mSelectedPlayer.mGoal.Equals(tile))
                        DrawTile(position, tile.mDimension, tile.mType, tile.mOrientation, iSelectedPlayer.mColor);
                    else if (showPath && iState.mSelectedPlayer.mPathFinder.IsTileInTree(iState.mChessboard.board[i * 7 + j]))
                        DrawTile(position, tile.mDimension, tile.mType, tile.mOrientation, Color.Yellow);
                    else
                        DrawTile(position, tile.mDimension, tile.mType, tile.mOrientation, Color.White);
                }

            DrawRemainedTile(iSelectedPlayer, iRemainedTile);
        }

        public void DrawPlayers(Viewport iViewport)
        {
            LabyrinthGameState.State iState = LabyrinthGameState.gameState;
            float tileLenght = iViewport.Height * 0.111111f;
            float playerLenght = tileLenght * 0.53f;

            for (int i = 0; i < iState.mPlayers.Length; ++i)
            {
                Player.Player p = iState.mPlayers.ElementAt(i);
                Vector2 tilePos = new Vector2(p.mPosition.mPosition.X, p.mPosition.mPosition.Y);
                Vector2 position = new Vector2(tilePos.Y * tileLenght + tileLenght * 1.5f, 0 + tilePos.X * tileLenght + tileLenght);
                position += new Vector2(tileLenght * 0.5f, tileLenght * 0.5f);

                Texture2D tex;
                if (p.mColor == Color.Red)
                    tex = mRenderWrapper.mTextureData.playerRed;
                else if (p.mColor == Color.Blue)
                    tex = mRenderWrapper.mTextureData.playerBlue;
                else
                    tex = mRenderWrapper.mTextureData.playerGreen;

                mRenderWrapper.DrawTexture(tex, new Vector2(playerLenght, playerLenght), position, 0, Color.White);
            }

            Player.Player pSelected = iState.mSelectedPlayer;
            Vector2 tilePosSelected = new Vector2(pSelected.mPosition.mPosition.X, pSelected.mPosition.mPosition.Y);
            Vector2 positionSelected = new Vector2(tilePosSelected.Y * tileLenght + tileLenght * 1.5f, 0 + tilePosSelected.X * tileLenght + tileLenght);
            positionSelected += new Vector2(tileLenght * 0.5f, tileLenght * 0.5f);

            Texture2D texSelected;
            if (pSelected.mColor == Color.Red)
                texSelected = mRenderWrapper.mTextureData.playerRed;
            else if (pSelected.mColor == Color.Blue)
                texSelected = mRenderWrapper.mTextureData.playerBlue;
            else
                texSelected = mRenderWrapper.mTextureData.playerGreen;

            mRenderWrapper.DrawTexture(texSelected, new Vector2(playerLenght, playerLenght), positionSelected, 0, Color.White);
        }

        void DrawRemainedTile(Player.Player iPlayer, Tile.Tile iRemainedTile)
        {
            Color color;

            if (iPlayer.mGoal == iRemainedTile)
                color = iPlayer.mColor;
            else
                color = Color.White;

            float xPos = mRenderWrapper.graphics.Viewport.Width - iRemainedTile.mDimension * 2.5f;
            float yPos = mRenderWrapper.graphics.Viewport.Height / 3.5f;
            Vector2 pos = new Vector2(xPos, yPos);

            DrawTile(
                pos,
                iRemainedTile.mDimension * 2,
                iRemainedTile.mType,
                iRemainedTile.mOrientation,
                color);
        }
        
    }
}
